package com.lanyou.esb.web.serviceMonitor;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.common.CommonUtils;
import com.lanyou.esb.common.Constants4Redis;
import com.lanyou.esb.service.ConcurrencyNumberService;
import com.lanyou.esb.service.CookProxyServiceTimeService;
import com.lanyou.esb.vo.ConcurrencyNumberVo;

/**
 * 并发数控制器
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/concurrencyNumber")
public class ConcurrencyNumberController {
	@Autowired
	private ConcurrencyNumberService concurrencyNumberService;

	@Autowired
	private CookProxyServiceTimeService cookProxyServiceTimeService;

	/** 延时读取服务数据时间 **/
	private static int LAZY_TIME = CommonUtils.CONCURRENCY_NUMBER_TIME_SEPARATOR * 5;
	/** 读取数据区间个数 **/
	private static int SIZE = 10;

	/**
	 * 查询并发数
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/findConcurrencyNumberInfo", produces = "application/json")
	public String findConcurrencyNumberInfo() {
		Map<String, Object> result = new HashMap<>(2);
		// COOKPROXY服务器时间
		Date serviceTime = null;
		try {
			// 获取COOKPROXY服务器时间
			Calendar calendar = cookProxyServiceTimeService
					.getCookProxyServiceTime();
			if (calendar != null) {
				// 延时读数据
				calendar.add(Calendar.SECOND, -LAZY_TIME);
				serviceTime = calendar.getTime();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (serviceTime == null) {
			serviceTime = new Date();
		}
		try {
			// 查询并发数
			ConcurrencyNumberVo vo = concurrencyNumberService
					.findConcurrencyNumberInfo(serviceTime, SIZE);
			if (vo != null && vo.getEntrys() != null) {
				for (com.lanyou.esb.vo.ConcurrencyNumberVo.Entry entry : vo
						.getEntrys()) {
					if (entry.getValue() < 0) {
						entry.setValue(0);
					}
				}
			}
			// 记录最后一个时间点
			result.put(CommonUtils.MSG_FIELD_LASTTIMEPOINT,
					vo.getEntrys().get(vo.getEntrys().size() - 1).getKey());
			result.put(CommonUtils.MSG_FIELD_RESULT, true);
			result.put(CommonUtils.MSG_FIELD_VALUE, vo);
		} catch (Exception e) {
			result.put(CommonUtils.MSG_FIELD_RESULT, false);
			result.put(CommonUtils.MSG_FIELD_MSG, e.getMessage());
		}
		return GsonUtil.getDefaultGson().toJson(result);
	}

	/**
	 * 获取某个时间点的并发数
	 * 
	 * @param lastTimePoint
	 *            ：上次获取数据的时间点
	 * @return
	 */
	@RequestMapping(value = "/findLastConcurrencyNumberInfo", produces = "application/json")
	public String findLastConcurrencyNumberInfo(String lastTimePoint) {
		// 查询的目标时间点
		Calendar calendar = Calendar.getInstance();
		Date now = new Date();
		// 并发数在REDIS中保存的最早时间点
		Calendar timeout = Calendar.getInstance();
		timeout.setTime(now);
		timeout.set(Calendar.MILLISECOND, 0);
		timeout.add(Calendar.SECOND,
				-Constants4Redis.REDIS_TIMEOUT_CONCURRENCY_NUMBER);
		Calendar save = Calendar.getInstance();
		try {
			save.setTimeInMillis(Long.parseLong(lastTimePoint));
			calendar.setTimeInMillis(Long.parseLong(lastTimePoint));
		} catch (Exception e) {
			calendar.setTime(now);
		}
		Map<String, Object> result = new HashMap<>(2);
		// 每隔一秒取数据
		calendar.add(Calendar.SECOND,
				CommonUtils.CONCURRENCY_NUMBER_TIME_SEPARATOR);
		try {
			double value = concurrencyNumberService
					.findLastConcurrencyNumberInfo(calendar.getTime());
			if (value < 0) {
				value = 0;
			}
			BigDecimal b = new BigDecimal(value);
			value = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
			result.put(CommonUtils.MSG_FIELD_RESULT, true);
			result.put(CommonUtils.MSG_FIELD_KEY, calendar.getTimeInMillis());
			result.put(CommonUtils.MSG_FIELD_VALUE, value);
		} catch (Exception e) {
			result.put(CommonUtils.MSG_FIELD_RESULT, false);
			result.put(CommonUtils.MSG_FIELD_MSG, e.getMessage());
		}
		return GsonUtil.getDefaultGson().toJson(result);
	}
}
